home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4244 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mudskipper.cac.psu.edu!user
  2. From: fcusack@tdx.org (frank.)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: Fri, 02 Feb 1996 16:21:47 -0400
  6. Organization: Penn State University, Center for Academic Computing
  7. Message-ID: <fcusack-0202961621470001@mudskipper.cac.psu.edu>
  8. References: <11f7cc$17261a.3b3@daprez> <4etj7c$bma@news.iag.net>
  9. NNTP-Posting-Host: mudskipper.cac.psu.edu
  10.  
  11. In article <4etj7c$bma@news.iag.net>, jatmon@iag.net (John R Buchan) wrote:
  12.  
  13. > In article <11f7cc$17261a.3b3@daprez>, otisg@panther.middlebury.edu says...
  14. > >
  15. > >Hi, a C question...
  16. > >this one has been bothering ALL afternoon !
  17. > >
  18. > >This snippet of code is supposed to call appropriate function based on what
  19. > >command line arguments are supplied.
  20. > >
  21. > >usage: <program> -e [SourceFile] RemoteFile
  22. > >        or
  23. > >       <program> -d [InFile]
  24. > >
  25. > >if the person doesn't use this right the program is supposed to call Usage()
  26. > >which is not here, but it's in my code, and if the person calls the program
  27. > >the correct way one of the two actions should be taken (encode or decode).
  28. > >
  29. > >Problem - I am doing something wrong and I almost always end up calling
  30. > >Usage() even if I use the program correctly.
  31. > >
  32. > >#include <stdio.h>
  33. > >#include <stdlib.h>
  34. > >#include <string.h>
  35. > >
  36. > >int main (int argc, char **argv) {
  37. > >
  38. > >  void Usage   (void);
  39. > >  void Encode (int, char **);
  40. > >  void Decode (int, char **);
  41. > >
  42. > >  if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
  43. > >    Usage();
  44. > >  }
  45. > <snip>
  46. > I assume you mean this to call Usage if argv[1] is not "-e" and not "-d"?
  47. > Change the || to &&.  
  48.  
  49. The && test would _never_ pass. argv[1] could never be both "-d" and "-e".
  50. The || is correct here.
  51.  
  52. >Of course, if no argument was passed, who knows what
  53. > will happen here.  You should always test argc first to be certain that 
  54. > the argv element exists.
  55.  
  56. argv[1] is always guaranteed to exist. It is simply NULL if there were no
  57. arguments.
  58. ~Frank
  59.  -- I am Pentium of Borg.  Division is futile.  You will be approximated. --
  60.  --   If you build it, they will come --> http://www.tdx.org/~fcusack/    --
  61.  -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F  3F 64 80 65 8B 0F F9 EA --
  62.